package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cqupt.qualityevaluation.dao.expertMapper;
import com.cqupt.qualityevaluation.pojo.User;
import com.cqupt.qualityevaluation.pojo.expert;
import com.cqupt.qualityevaluation.pojo.schooluser;
import com.cqupt.qualityevaluation.utils.jwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/expert")
public class ExpertController {
    @Autowired
    private expertMapper expertMapper;
    @GetMapping("/findAll")
    public List<expert> findAll(){
        return expertMapper.selectList(null);
    }
    @PostMapping("/save")
    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
    public Object save(@RequestBody expert ex){
        Map map=new HashMap<>();
        int result=0;
        try {
            result=expertMapper.insert(ex);
        }catch (org.springframework.dao.DuplicateKeyException e){
            map.put("code","fail");
            map.put("msg","该角色名已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","添加成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @DeleteMapping("/delete/{username}")
    public String delete(@PathVariable("username") String username){
        QueryWrapper<expert> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        expertMapper.delete(queryWrapper);
        return "success";
    }
    @PutMapping("/update")
    public Object update(@RequestBody expert ex){
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<expert> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",ex.getId());
        expert expert = new expert();
        expert.setUsername(ex.getUsername());
        expert.setPassword(ex.getPassword());
        expert.setRealname(ex.getRealname());
        expert.setWorkplace(ex.getWorkplace());
        expert.setBankaccount(ex.getBankaccount());
        expert.setBanklocation(ex.getBanklocation());
        expert.setTitle(ex.getTitle());
        try {
            result=expertMapper.update(expert,updateWrapper);
        }catch (org.springframework.dao.DuplicateKeyException e){
            map.put("code","fail");
            map.put("msg","该角色名已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    //    @PutMapping("/update")
//    public int update(@RequestBody User u){
//        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("id",u.getId());
//        User user = new User();
//        user.setUsername(u.getUsername());
//        user.setPassword(u.getPassword());
//        return expertMapper.update(user,updateWrapper);
//    }
    @GetMapping("/selectone/{id}")
    public expert findByexpertId(@PathVariable("id") String id){
        QueryWrapper<expert> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return expertMapper.selectOne(queryWrapper);
    }
    @GetMapping("/search/1/{username}")
    public List<expert> findByexpertUsername(@PathVariable("username") String username){
        QueryWrapper<expert> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",username);
        return expertMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/2/{password}")
    public List<expert> findByexpertPassword(@PathVariable("password") String password){
        QueryWrapper<expert> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("password",password);
        return expertMapper.selectList(queryWrapper);
    }
}
